package com.spring.gll.fileuplodedown;

import com.spring.gll.utils.DownUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.FileInputStream;

@Controller
@RequestMapping("/fileDownload")
public class FileDownload {
    @RequestMapping("/view")
    public String view(){
        return "UplodeDownlode";
    }
    @RequestMapping("/down")
    public ResponseEntity<byte[]> down() throws Exception {
//        要下载的文件的路径
        String path = "E:/1.jpg";
//        将字符串转换为文件
        File file = new File(path);
//        文件输入字节流
        FileInputStream is = new FileInputStream(file);
//        is.available()  字节流的长度
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        HttpHeaders headers = new HttpHeaders();
//        以附件的形式添加响应
        headers.add("Content-Disposition",
                "attchement;filename="+file.getName());
//        响应状态
        HttpStatus status = HttpStatus.OK;
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes, headers, status);
        return responseEntity;
    }
    @RequestMapping("/down2")
    public ResponseEntity<byte[]> down2(String file) throws Exception {
        return DownUtil.down(file);
    }
}
